<template>
  <div>
    <el-drawer
      :title="'项目风险'"
      :visible.sync="proFileOpen"
      :before-close="cancel"
      size="68%"
      class="always_title"
    >
<!--      :withHeader="true"-->
      <div style="padding: 0 30px; height: 93%">
        <el-form
          :model="formData"
          label-position="center"
          label-width="120px"
          :size="medium"
        >

          <el-row>
            <el-col :span="8" class="grid-cell">
              <el-form-item label="项目名称" prop="projectName">
                <el-input
                  disabled
                  v-model="formData.projectName"
                  type="text"
                  clearable
                  :size="medium"
                  placeholder="请输入项目名称"
                ></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="8" class="grid-cell">
              <el-form-item label="文件类型" prop="fileType">
                <el-input
                  v-model="formData.fileType"
                  type="text"
                  disabled
                  clearable
                  :size="medium"
                  placeholder="请输入文件类型"
                ></el-input>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <el-table
          :data="tableData"
          border
        >
          <el-table-column type="selection" width="40">
          </el-table-column>
          <el-table-column
            prop="oldFileName"
            label="文件名"
          >
            <template slot-scope="scope">
              <a @click.prevent="handlePictureCardPreview(scope.row)" style="color: #409eff">
                <span>{{ scope.row.oldFileName }}</span>
              </a>
            </template>
          </el-table-column>
          <el-table-column
            prop="fileSize"
            label="大小"
            width="130"
          ></el-table-column>
          <el-table-column
            prop="createByName"
            label="创建人"
            width="100"
          ></el-table-column>
          <el-table-column
            prop="createTime"
            label="创建时间"
            width="180"
          ></el-table-column>
          <el-table-column prop="size" label="操作" width="150">
            <template slot-scope="{ row }">
              <el-button @click="handlePictureCardPreview(row)">预览</el-button>
              <el-button @click="downloadFile(row)">下载</el-button>
            </template>
          </el-table-column>
        </el-table>
        <div slot="footer" class="dialog-footer" style="display: flex; justify-content: center; padding: 20px 0;">
          <el-button @click="cancel">取 消</el-button>
        </div>
      </div>
    </el-drawer>
  </div>
</template>

<script>

import moment from "moment";
import {getRisk} from "@/api/system/mis/risk";
import {getInitmsn} from "@/api/system/mis/initmsn";
import {download, getFilePath, preview} from "@/api/system/sysFileInfo/fileInfo";
import {getFile, onlinePreview} from "@/api/system/mis/file";

export default {
  name: "ProSelect",
  components: {  },
  dicts: ["sys_normal_disable", "sys_user_sex"],
  props: {
    width: {
      type: String,
      default: "900px",
    },
    height: {
      type: String,
      default: "650px",
    },
    proFileOpen: {
      type: Boolean,
      default: false,
    },
    id: {
      type: String,
      default: undefined,
    },
  },
  computed: {},
  data() {
    return {
      formData: {},
      // 总条数
      total: 0,
      medium:'medium',
      size: '',
      tableData: [],
      loading: false,
    };
  },
  filters: {
    formatDate: function(value) {
      if (value) {
        return moment(value).format('YYYY-MM-DD')
      }
    }
  },
  created() {
    this.getList();
  },
  watch: {
    id(row){
      this.getList()
    },
  },
  methods: {
    /** 查询项目列表 */
    getList() {
      this.loading = true;
      if (this.id !== undefined){
        getFile(this.id).then(response => {
          let data = {
            ids: this.id,
          };
          this.formData = response.data;
          this.tableData = []
          getFilePath(data).then((res) => {
            res.data.forEach((item) => {
              item.fileSize += "KB";
              this.tableData.push({
                businessKey: item.businessKey,
                fileName: item.fileName,
                createByName: item.createByName,
                createTime: item.createTime,
                oldFileName: item.oldFileName,
                fileSize: item.fileSize,
                fileUrl: item.fileUrl,
                ID: item.id,
              });
              console.log(this.tableData)
            });
          });
        });
      }
    },
    // 预览
    handlePictureCardPreview(row){
      this.$modal.loading("正在加载...");
      const fileExtension = row.oldFileName.split('.').pop();
      if (fileExtension === 'png' || fileExtension === 'jpg') {
        preview(row.ID).then((s) =>{
          if (s != ""){
            this.dialogImageUrl = window.URL.createObjectURL(new Blob([s], { type: "image/jpeg" }));
            this.fileDialogVisible = true;
            this.$modal.closeLoading();
            window.open( this.dialogImageUrl)
            return;
          }
        })
      }else if (fileExtension === 'pdf'){
        preview(row.ID).then((response) =>{
          let blob = new Blob([response], { type: 'application/pdf;charset=utf-8' });
          const href = URL.createObjectURL(blob);
          this.$modal.closeLoading();
          window.open(href, 'newWindow');
        })
      } else if (fileExtension === 'xlsx' || fileExtension === 'xls'){
        this.$confirm('此类型文件不可预览, 是否下载?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.downloadFile(row)
        }).catch(() => {

        });
      } else {
        onlinePreview(row.ID).then((response) => {
          let blob = new Blob([response], { type: 'application/pdf;charset=utf-8' });
          const href = URL.createObjectURL(blob);
          this.$modal.closeLoading();
          window.open(href, 'newWindow');
        })
      }
    },
    // 删除
    downloadFile(row){
      this.$modal.loading("下载中")
      download(row.ID).then((rew) =>{
        this.$modal.closeLoading();
        // 创建临时的下载链接
        const url = window.URL.createObjectURL(rew);
        if (typeof window.chrome !== 'undefined') {
          // chrome
          const link = document.createElement('a');
          link.href = url;
          link.download = row.oldFileName;
          document.body.appendChild(link);
          link.click();
          setTimeout(() => {
            document.body.removeChild(link);
            URL.revokeObjectURL(url);
          }, 100);
        } else if (typeof window.navigator.msSaveBlob !== 'undefined') {
          // IE
          window.navigator.msSaveBlob(rew,  row.oldFileName);
        } else {
          // Firefox
          const file = new File([rew],  row.oldFileName,{ type: 'application/octet-stream' });
          window.open(URL.createObjectURL(file));
        }
      }).finally(() =>{
      })
    },
    cancel() {
      this.$emit("cancel");
    },
  },
};
</script>
<style>
.el-dialog__wrapper > .el-dialog {
  border-radius: 26px;
  height: 650px;
}
</style>
<style lang="scss" scoped>

.always_title {
  font-size: 20px;
  font-weight: bold;
  line-height: 53px;
  margin-left: 30px;
}
.always_title1 {
  font-size: 18px;
  font-weight: bold;
  // line-height: 53px;
  margin-left: 15px;
  position: relative;
}

.always_title1 ::before {
  content: "|";
  flex-shrink: 0;
  width: 4px;
  height: 6%;
  margin-left: 12px;
  // background: #f90;
  color: #f90;
  position: absolute;
  left: -22px;
  margin-bottom: 12px;
}

.selectBox {
  width: 100%;
  min-width: 900px;
  display: flex;
  flex-direction: column;

  .topBox {
    width: 100%;
    display: flex;
    margin-top: 10px;
    .leftBox {
      width: 280px;
      height: 400px;
      margin-right: 10px;

      -webkit-box-shadow: 0 2px 4px 0 rgba(0, 0, 0, 0.12),
        0 0 6px 0 rgba(0, 0, 0, 0.04);
      box-shadow: 0 2px 4px 0 rgba(0, 0, 0, 0.12), 0 0 6px 0 rgba(0, 0, 0, 0.04);
      display: flex;
      flex-direction: column;

      .leftBox_title {
        width: 100%;
        height: 40px;
        font-size: 16px;
        line-height: 40px;
        padding-left: 10px;
        background-color: #f5f5f5;
        position: relative;
        .leftBox_title_do {
          position: absolute;
          right: 16px;
          top: 8px;
        }
      }

      .contentBox {
        width: 100%;
      }
    }
  }

  .bottomBox {
    margin-top: 15px;
    width: 100%;
    height: 75px;
    // border: 1px solid #DCDFE6;
    // -webkit-box-shadow: 0 2px 4px 0 rgba(0, 0, 0, 0.12), 0 0 6px 0 rgba(0, 0, 0, 0.04);
    // box-shadow: 0 2px 4px 0 rgba(0, 0, 0, 0.12), 0 0 6px 0 rgba(0, 0, 0, 0.04);
    // overflow-y: auto;

    .selectPeopleList {
      width: 100%;
      display: flex;
      flex-wrap: wrap;

      .selectPeopleCard {
        width: 50px;
        height: 56px;
        display: flex;
        flex-direction: column;
        align-items: center;
        margin: 5px 10px;
        .user-avatar {
          width: 40px;
          height: 40px;
          border-radius: 50%;
          color: #fff;
          background: #ffa502;
          font-size: 14px;
          text-align: center;
          line-height: 40px;
        }

        .userInfo {
          width: 100%;
          display: flex;
          flex-direction: column;
          margin-left: 5px;
          margin-right: 5px;

          div:nth-child(1) {
            font-size: 14px;
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
            width: 100%;
            text-align: center;
          }
        }
      }
    }
  }
}

.treeBox {
  width: 100%;
  height: 320px;
  overflow-x: hidden;
  overflow-y: auto;
}
.el-dialog{
  width: 900px;
  height: 650px;
}
.ellipsis {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
::v-deep .el-drawer__header {
  height: 35px;
  margin: 20px 30px;
  border-radius: 4px;
  border-left: 5px solid #0169ff;
  background-color: white;
  line-height: 34px;
  padding-left: 18px;
  color: #0169ff;
  background-image: linear-gradient(90deg, #f2f7ff 0%, #ffffff 95%);
  border-radius: 4px;
  font-size: 18px;
  font-weight: 500;
  padding-top: 0px;
}
::v-deep .el-drawer__header {
  min-height: 7% !important;
  height: auto;
}
//.el-dialog .dialog-paging {
//  position: absolute;
//  bottom: 40px;
//  width: 96%;
//  padding: 10px 20px; /* 根据需要调整内边距 */
//  text-align: right;
//  border-top: 0px solid #ebeef5;
//}
.peopleBox {
  width: 100%;
  height: 360px;
  overflow-x: hidden;
  overflow-y: auto;

  .peopleList {
    width: 100%;
    height: auto;

    .peopleCard:hover {
      background-color: #fafafa;
      transition: 0.2s;

      .peopleName {
        color: #eb6100;
      }
    }
    .peopleCard {
      width: 100%;
      height: 48px;
      border-top: solid #f5f5f5 1px;
      border-bottom: solid #f5f5f5 1px;
      padding-left: 8px;
      display: flex;
      align-items: center;
      cursor: pointer;
      transition: 0.2s;
      ::v-deep .el-checkbox__label {
        display: flex;
        align-items: center;
      }

      .avatarBox {
        width: 40px;
        height: 40px;

        .user-avatar {
          width: 40px;
          height: 40px;
          border-radius: 50%;
          color: #fff;
          font-size: 16px;
          text-align: center;
          line-height: 40px;
          user-select: none;
        }
      }

      .peopleInfoBox {
        width: 190px;
        height: 100%;
        display: flex;
        flex-direction: column;
        padding-left: 8px;

        .peopleName {
          width: 100%;
          height: 20px;
          white-space: nowrap;
          overflow: hidden;
          text-overflow: ellipsis;
          font-size: 16px;
          line-height: 20px;
        }

        .peopleDept {
          width: 100%;
          height: 20px;
          white-space: nowrap;
          overflow: hidden;
          text-overflow: ellipsis;
          font-size: 14px;
          line-height: 20px;
          color: #999;
        }
      }

      .peopleDeleteBtn {
        width: 20px;
        height: 20px;
        // border-radius: 50%;
        // background: url(./delete2.png) no-repeat;
        background-size: 100% 100%;
        transition: 0.2s;
      }

      .peopleDeleteBtn:hover {
        width: 20px;
        height: 20px;
        // border-radius: 50%;
        // background: url(./delete1.png) no-repeat;
        background-size: 100% 100%;
        transition: 0.2s;
      }
    }
  }
}
</style>
